<template lang="pug">
.view.cp-man-train-agency.fix-flex-pos
  el-form.manta-search(:inline="true")
    el-form-item
      el-select.select-width(v-model="kwType")
        el-option(label="机构编号", :value="1")
        el-option(label="机构名称", :value="2")
        el-option(label="联系人", :value="3")
        el-option(label="联系电话", :value="4")
    el-form-item
      el-input(v-model="keyWord", placeholder="请输入", style="width: 200px")
    el-form-item(label="状态 :")
      el-select.select-width(v-model="status", placeholder="请选择")
        el-option(label="全部", :value="null")
        el-option(v-for="(item, index) in statusSelect", :key="index", :label="item.name", :value="item.value")
    el-form-item
      el-button(type="primary", @click="search") 搜索
    el-form-item
      el-button(type="primary", @click="creatTrainAgency") 新建
    //- el-form-item
      el-button(@click="exportReport", plain) 导出
  //- 表格
  .manta-table
    el-table(
      :data="dataList",
      highlight-current-row,
      v-loading="loadingData",
      size="mini",
      height="100%")
      el-table-column(show-overflow-tooltip, type="index", label="序号")
      el-table-column(show-overflow-tooltip, prop="agName", label="机构名称")
      el-table-column(show-overflow-tooltip, prop="agCode", label="机构编号")
      el-table-column(show-overflow-tooltip, prop="agDesc", label="机构简介")
      el-table-column(show-overflow-tooltip, prop="agAddress", label="机构地址")
      el-table-column(show-overflow-tooltip, prop="agContacts", label="联系人")
        template(slot-scope="scope")
          span {{ scope.row.agContacts | compare-list(empSelect) }}
      el-table-column(show-overflow-tooltip, prop="agContactNumber", label="联系电话")
      el-table-column(show-overflow-tooltip, prop="agClassNumber", label="教室数量")
      el-table-column(show-overflow-tooltip, prop="agTeacherNumber", label="老师数量")
      el-table-column(show-overflow-tooltip, label="状态")
        template(slot-scope="scope")
          span {{ scope.row.status | compare-list(statusSelect) }}
      el-table-column(label="操作", min-width="100px")
        template(slot-scope="scope")
          el-tooltip(content="查看")
            i.icon-act.el-icon-view(@click="viewTrainAgency(scope.row, scope.$index)")
          el-tooltip(content="编辑")
            i.icon-act.el-icon-edit(@click="editTrainAgency(scope.row, scope.$index)")
          el-tooltip(content="删除")
            i.icon-act.el-icon-delete(@click="deleteTrainAgency(scope.row, scope.$index)")
  //- 分页
  el-pagination.manta-pagination(
    background,
    layout="total, prev, pager, next, jumper",
    prev-text="上一页",
    next-text="下一页",
    @current-change="handleCurrentChange",
    :current-page="pageNum",
    :page-size="pageSize",
    :total="totalNum")
  //- 新建机构
  agency-popup(
    :title="taTitle",
    :visible.sync="taVisible",
    :is-edit="taEdit",
    :parent-data="formData",
    @submit="onFormSubmit",
    @close="taVisible = false")
</template>

<script>
import { mapGetters } from 'vuex';
import TrainData from '@/data/train/trainManage';
import AgencyPopup from './agencyPopup';

export default {
  name: 'mantrainagency',
  components: {
    AgencyPopup
  },
  data() {
    return {
      loadingData: true,
      dataList: [],
      pageNum: 1,
      pageSize: 16,
      totalNum: 0,
      // 搜索条件
      kwType: 1,
      keyWord: null,
      status: null,
      // 弹出框
      taTitle: '新建培训机构',
      taVisible: false,
      taEdit: false,
      formData: {},
      // 搜索下拉框
      statusSelect: [
        { name: '启用', value: 1 },
        { name: '禁用', value: 0 }
      ]
    };
  },
  computed: {
    ...mapGetters([
      'empSelect',
      'teacherSelect'
    ])
  },
  created() {
  },
  mounted() {
    this.loadDataList();
  },
  methods: {
    // 整理参数
    filterParams() {
      const params = {
        kwType: this.kwType,
        keyWord: this.keyWord,
        status: this.status
      };
      for (const key in params) {
        if (params[key] === null || params[key] === '') {
          delete params[key];
        }
      }
      return params;
    },
    // 取得表格数据
    loadDataList() {
      this.loadingData = true;
      // const params = this.filterParams();
      setTimeout(() => {
        this.dataList = TrainData.trainAgencyList;
        this.totalNum = 64;
        this.loadingData = false;
      }, 500);
    },
    // 搜索
    search() {
      if (this.pageNum !== 1) {
        this.pageNum = 1;
      }
      this.loadDataList();
    },
    // 页码改变
    handleCurrentChange(pageNum) {
      this.pageNum = pageNum;
      this.loadDataList();
    },
    // 新建培训机构
    creatTrainAgency() {
      this.taTitle = '新建培训机构';
      this.taVisible = true;
      this.taEdit = true;
      this.formData = {};
    },
    // 查看
    viewTrainAgency(row, index) {
      console.log('viewTrainAgency', row, index);
      this.taTitle = '查看培训机构';
      this.taVisible = true;
      this.taEdit = false;
      this.formData = {};
    },
    // 编辑
    editTrainAgency(row, index) {
      console.log('editTrainAgency', row, index);
      this.taTitle = '编辑培训机构';
      this.taVisible = true;
      this.taEdit = true;
      this.formData = {};
    },
    // 删除
    deleteTrainAgency(row, index) {
      console.log('deleteTrainAgency', row, index);
      this.$confirm('此操作将永久删除该条记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        setTimeout(() => {
          if (Math.random() > 0.5) {
            this.dataList.splice(index, 1);
            this.totalNum = this.totalNum - 1;
            this.$message({ message: '删除成功', type: 'success' });
          } else {
            this.$message.error('删除失败');
          }
        }, 500);
      }).catch(() => {
        this.$message({ type: 'info', message: '已取消删除' });
      });
    },
    // 保存提交成功
    onFormSubmit(data) {
      if (data.isNew) {
        // 新增
        this.dataList.unshift({});
        this.$set(this.dataList, 0, data);
        this.totalNum = this.totalNum + 1;
      } else {
        const index = this.dataList.findIndex(item => item.orderNo === data.orderNo);
        this.$set(this.dataList, index, data);
      }
    }
  }
};
</script>

<style lang="sass">
@import "../../../../styles/utils"
$SEARCHHEIGHT: 56px
$SELECTWIDTH: 100px
$PADDINGWIDTH: 16px
.cp-man-train-agency
  .manta-search
    height: $SEARCHHEIGHT
    padding: 12px 0 12px 16px
    overflow-x: auto
    overflow-y: hidden
    .select-width
      width: $SELECTWIDTH
    .date-width
      width: 260px
    .el-form-item
      margin-right: 14px
    .el-form-item__label
      margin-left: 6px
      padding-right: 6px
    .el-date-editor .el-range__close-icon
      width: 20px
    .el-form-item--small .el-form-item__content, .el-form-item--small .el-form-item__label
      height: 32px
  .manta-table
    position: absolute
    top: $SEARCHHEIGHT
    bottom: 56px
    left: 16px
    right: 16px
    overflow-x: hidden
    overflow-y: auto
    &::before
      display: none
    .icon-act
      font-size: 16px
      font-weight: 500
      cursor: pointer
      margin-left: 3px
      margin-right: 3px
      &:hover
        color: #5D9BFB
  .el-table
    th, td
      text-align: center
  .manta-pagination
    position: absolute
    bottom: 12px
    left: 0
    right: 0
    text-align: center
</style>
